package com.wy.w_ui.banner.indicator;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.w_library.utils.DisplayUtil;
import com.wy.w_ui.R;

/**
 * banner 圆形指示器
 */
public class WCircleIndicator extends FrameLayout implements IWIndicator {

    private static final int VWC = ViewGroup.LayoutParams.WRAP_CONTENT;

    /**
     * 未选中指示器
     */
    private int pointNormal = R.drawable.banner_indicator_normal;
    /**
     * 选中指示器
     */
    private int pointSelected = R.drawable.banner_indicator_selected;
    /**
     * 指示器左右间距
     */
    private int pointLeftRightPadding;
    /**
     * 指示器上下间距
     */
    private int pointTopBottomPadding;

    public WCircleIndicator(@NonNull Context context) {
        this(context,null);
    }

    public WCircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public WCircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        pointLeftRightPadding = DisplayUtil.dp2Px(getContext(),5);
        pointTopBottomPadding = DisplayUtil.dp2Px(getContext(),15);
    }


    @Override
    public View get() {
        return this;
    }

    @Override
    public void onInflate(int count) {
        removeAllViews();
        if (count <= 0 ){
            return;
        }

        LinearLayout groupView = new LinearLayout(getContext());
        groupView.setOrientation(LinearLayout.HORIZONTAL);

        ImageView imageView;
        LinearLayout.LayoutParams lPImageView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lPImageView.gravity = Gravity.CENTER_VERTICAL;
        lPImageView.setMargins(pointLeftRightPadding,pointTopBottomPadding,pointLeftRightPadding,pointTopBottomPadding);

        for (int i = 0; i < count; i++) {
            imageView = new ImageView(getContext());
            imageView.setLayoutParams(lPImageView);
            if (i == 0){
                imageView.setImageResource(pointSelected);
            }else {
                imageView.setImageResource(pointNormal);
            }
            groupView.addView(imageView);
        }

        LayoutParams lpViewGroup = new LayoutParams(VWC,VWC);

        lpViewGroup.gravity = Gravity.CENTER | Gravity.BOTTOM;
        addView(groupView,lpViewGroup);

    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            ImageView imageView = (ImageView) viewGroup.getChildAt(i);
            if (current == i){
                imageView.setImageResource(pointSelected);
            }else {
                imageView.setImageResource(pointNormal);
            }
            imageView.requestLayout();
        }
    }

}
